import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

const List<String> supportFileType =["docx","doc","xlsx","xls","pptx","ppt","pdf","txt","jpg","jpeg","png"];

class FilePreView extends StatefulWidget {
  final String? path;
  final String? url;
  const FilePreView({Key? key,this.path, this.url}) : super(key: key);
  


  @override
  _FilePreViewState createState() => _FilePreViewState();
}

class _FilePreViewState extends State<FilePreView> {
  final String viewType = 'ac/file_preview';
  bool? _support;
  
  @override
  void initState() {
   
    super.initState();
  }
  
  bool _didLoaded = false;

  @override
  Widget build(BuildContext context) {
    return Platform.isIOS?Container(
      width: double.infinity,
      height: double.infinity,
      child: Stack(
        children: [
          UiKitView(
            viewType: viewType,
            layoutDirection: TextDirection.ltr,
            onPlatformViewCreated: (int id)async{
              if(widget.path !=null){
                File file = File(widget.path!);
                if(file.existsSync()){
                  String type = widget.path!.split('.').last;
                  _support = false;
                  supportFileType.forEach((element) {
                    if(element.toUpperCase() ==  type.toUpperCase()){
                      _support = true;
                    }
                  });
                  if(_support!){
                    MethodChannel _cannel =MethodChannel('xyx/file_preview_$id');
                    _cannel.invokeMethod('load_file',{'path':widget.path});
                    _cannel.setMethodCallHandler((call){
                      if(call.method == 'file_load_progress'){
                        setState(() {
                            _didLoaded = true;
                          });
                      }
                      return Future.value();
                    });
                  }
                }
              }else if(widget.url != null){
                 _support = false;
                  supportFileType.forEach((element) {
                    String type = widget.url!.split('.').last;
                    if(element.toUpperCase() ==  type.toUpperCase()){
                      _support = true;
                    }
                  });
                  if(_support!){
                    MethodChannel _cannel =MethodChannel('xyx/file_preview_$id');
                    _cannel.invokeMethod('load_url',{'url':Uri.encodeFull(widget.url!)});
                    _cannel.setMethodCallHandler((call){
                      if(call.method == 'file_load_progress'){
                        if(call.arguments == 1.0){
                          setState(() {
                            _didLoaded = true;
                          });
                        }
                      }
                      return Future.value();
                    });
                  }
              }
              

            },
            creationParamsCodec: const StandardMessageCodec(),
          ),
          Center(
            child: Visibility(
              visible: !_didLoaded,
              child: CircularProgressIndicator(color: Color(0xFF5272f7),)
            ),
          )
        ],
      ),
    ):Center(
      child: Text('not support'),
    );
  }
}
